package controller;
 
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pojo.Book;
import pojo.Topic;

import java.util.*;

import dao.BookDAO;
import dao.TopicDAO;
 
@Controller
public class BookController {
 
	private static int btt=0;
	private static int topicId=0;
	private static int page=0;
	private static int kieuSort=0;
	
    @RequestMapping("/book.do")
    public String getsach(
    		@RequestParam(value="tpid", required = false) String _topicId,
			@RequestParam(value ="trang", required = false) String trang,
			@RequestParam(value ="slbooktt", required = false) String _btt,
			@RequestParam(value ="kieuSort", required = false) String _kieusort,
			Model model		
    		) {
    	if(_btt!=null)    	btt=Integer.parseInt(_btt);
    	if(_topicId!=null) topicId=Integer.parseInt(_topicId);
    	if(trang!=null) page=Integer.parseInt(trang);
    	if(_kieusort!=null) kieuSort=Integer.parseInt(_kieusort); 
    	TopicDAO td= new TopicDAO();
    	Topic tp= td.getTopicByID(topicId);
    	
    	BookDAO bd= new BookDAO();
    	long tongsotrang= bd.tinhSoTrangTheoTopicCap1(tp, btt);
    	List<Book> lb= bd.getBookByTopic(tp, page,btt,kieuSort);
        Topic menu= td.getTopicByID(topicId);
   
        Topic menu2=null;
        Topic menu1=null;
        if(tp.getParentId()!=0)
        {
        	menu2=tp;
        	menu1=td.getTopicByID(tp.getParentId());
        }
        else
        {
        	menu1=tp;
        }
        List<Topic> lt2=td.getMenu2(menu1);
        if(lt2.size()==0) lt2=null;
    	model.addAttribute("menu1",menu1);
    	model.addAttribute("menu2",menu2);
    	model.addAttribute("lmenu2", lt2);
    	model.addAttribute("lb",lb);
    	model.addAttribute("Servlet","book.do?tpid="+topicId);
    	model.addAttribute("tongSoTrang",tongsotrang);
    	model.addAttribute("trangHienTai",page);
    	model.addAttribute("slbooktt",btt);
    	model.addAttribute("kieuSort",kieuSort);
    	
    	model.addAttribute("title1",menu1.getTopicName());
        if(menu2!=null) model.addAttribute("title2",menu2.getTopicName());
        else model.addAttribute("title2",null);
        return ("page/FrontEnd/SachTopic");
    }
   

}